home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / EGAVGA.SWG / 0017_LINEDRAW.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  111 lines

  1. {1)  An efficient/optimised line-drawing routine (in Pascal
  2. or Asm) based on (or better than) the Bres. Line algorithm.
  3. }
  4.  
  5. {$R-,S-}
  6.  
  7. Uses
  8.   Crt, Dos;
  9.  
  10. Procedure PutPixel(X, Y : Word; Color : Byte);
  11. begin
  12.   Mem[$A000:Y*320+X] := Color
  13. end;
  14.  
  15. Procedure Switch(Var First, Second : Integer);
  16. { Exchange the values of First and second }
  17. Var
  18.   Temp : Integer;
  19. begin
  20.   Temp := First;
  21.   First := Second;
  22.   Second := Temp;
  23. end; { Switch }
  24.  
  25. Procedure Line(X1, Y1, X2, Y2, Color : Integer);
  26. { Uses Bressenham's algorithm For drawing a line }
  27. Var
  28.   LgDelta, ShDelta, LgStep, ShStep, Cycle, PointAddr : Integer;
  29.  
  30. begin
  31.   LgDelta := X2 - X1;
  32.   ShDelta := Y2 - Y1;
  33.   if LgDelta < 0 then
  34.     begin
  35.       LgDelta := -LgDelta;
  36.       LgStep := -1;
  37.     end
  38.   else
  39.     LgStep := 1;
  40.   if ShDelta < 0 then
  41.     begin
  42.       ShDelta := -ShDelta;
  43.       ShStep := -1;
  44.     end
  45.   else
  46.     ShStep := 1;
  47.   if LgDelta > ShDelta then
  48.     begin
  49.       Cycle := LgDelta shr 1; { LgDelta / 2 }
  50.       While X1 <> X2 do
  51.       begin
  52.         Mem[$A000:Y1*320+X1] := Color; { PutPixel(X1, Y1, Color); }
  53.         Inc(X1, LgStep);
  54.         Inc(Cycle, ShDelta);
  55.         if Cycle > LgDelta then
  56.         begin
  57.           Inc(Y1, ShStep);
  58.           Dec(Cycle, LgDelta);
  59.         end;
  60.       end;
  61.     end
  62.   else
  63.     begin
  64.       Cycle := ShDelta shr 1; { ShDelta / 2 }
  65.       Switch(LgDelta, ShDelta);
  66.       Switch(LgStep, ShStep);
  67.       While Y1 <> Y2 do
  68.       begin
  69.         Mem[$A000:Y1*320+X1] := Color; { PutPixel(X1, Y1, Color); }
  70.         Inc(Y1, LgStep);
  71.         Inc(Cycle, ShDelta);
  72.         if Cycle > LgDelta then
  73.         begin
  74.           Inc(X1, ShStep);
  75.           Dec(Cycle, LgDelta);
  76.         end;
  77.       end;
  78.     end;
  79. end; { Line }
  80.  
  81. Procedure SetMode(Mode : Byte);
  82. { Interrupt $10, sub-Function 0 - Set video mode }
  83. Var
  84.   Regs : Registers;
  85. begin
  86.   With Regs do
  87.   begin
  88.     AH := 0;
  89.     AL := Mode;
  90.   end;
  91.   Intr($10, Regs);
  92. end; { SetMode }
  93.  
  94. Var
  95.   x,y,d:Word;
  96.   r:Real;
  97.  
  98. begin   { example }
  99.   SetMode($13);  { 320x200 256 color mode For VGA and MCGA cards }
  100.   For d := 0 to 360 * 10 do
  101.   begin
  102.      r := (d * PI) * 0.1 / 180;
  103.      x := round(sin(r * 5) * 90) + 160;
  104.      y := round(cos(r) * 90) + 100;
  105.      line(160,100,x,y,x div 4);
  106.   end;
  107.   Repeat Until port[$60] = 1;    { hit esc to end }
  108.  
  109.   SetMode($03) { Text mode }
  110. end.
  111.